#include <cstdio>
#include "array.h"

#include <algorithm>




Array::Array()  : capacityCoef(1.5)
{
	size = 0;
	capacity = 100;
	data = new int[capacity];

}

Array::Array(int cap)  : capacityCoef(1.5)
{
	size = 0;
	capacity = cap;
	data = new int[capacity];

}


Array::Array(const Array &templAr)  : capacityCoef(templAr.capacity)
{
	std::cout << "constructor copy" << std::endl;
	capacity = templAr.getCapacity();
	size = templAr.getSize();
	data = new int[capacity];

	for (int i = 0; i < templAr.size; i++)
		data[i] = templAr.get(i);

}
	
Array& Array::operator= (Array& ar)
{
	Array *templAr = new Array(ar);
	
	swap(this,templAr);

	this->print();
	templAr->print();

	
	return *this;
}


Array:: ~Array()
{
	std::cout << "destructor" << std::endl;

	delete [] data;

	size = 0;
}

void Array::swap(Array *ar1, Array *ar2)
{
	std::swap(ar1->data, ar2->data);
	std::swap(ar1->size, ar2->size);
	std::swap(ar1->capacity, ar2->capacity);
	
}


int Array::get(size_t index) const
{
	return data[index];
}

void Array::realloc()
{
	
	capacity = capacityCoef * capacity;
	Array *tmpAr = new Array(capacity);
	swap(this, tmpAr);	
	size++;
	
}

void Array::set(size_t i, int v)
{
	if (i < size) data[i] = v;
	else data[i] = 0;

}

void Array::push_back(int v)
{
	if (size == capacity)
	{
		realloc();	
	}
	size++;
	std::cout << size << " " << capacity << std::endl;
	set(size - 1, v);
}

void Array::print()
{

	for (int i = 0; i < size; i++)
		std::cout << data[i] << " ";

	std::cout <<  std::endl;
}


